package com.fmsz.xmly.adapters.findrecom;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.fmsz.xmly.activitys.findrecom.AlbumDetailActivity;
import com.fmsz.xmly.activitys.findrecom.PlayActivity;
import com.fmsz.xmly.activitys.findrecom.SubjectDetailActivity;
import com.fmsz.xmly.activitys.findrecom.WebActivity;
import com.fmsz.xmly.entitys.findrecom.VP1DataEntity;

import java.util.List;

/**
 * Created by Administrator on 2016/12/3.
 */

public class FindRecomVP1Adapter extends PagerAdapter{
    private List<ImageView> list;
    private List<VP1DataEntity> vp1List;
    private Context context;
    public FindRecomVP1Adapter(Context context,List<ImageView> list, List<VP1DataEntity> vp1List) {
        this.context=context;
        this.list=list;
        this.vp1List=vp1List;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        //TODO 给每一张图片设置点击监听
        list.get(position).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int index=0;
                if (position==0 || position==list.size()-2){
                    index=vp1List.size()-1;
                }else if (position==1 || position==list.size()-1){
                    index=0;
                }else {
                    index=position-1;
                }
                VP1DataEntity entity = vp1List.get(index);
                String type = entity.getType();
                String data = entity.getData();
                if (type.equals("2")){
                    startIntent(AlbumDetailActivity.class,data);
                }else if (type.equals("3")){
                    startIntent(PlayActivity.class,data);
                }else if (type.equals("4") || type.equals("10")){
                    startIntent(WebActivity.class,data);
                }else if (type.equals("9")){
                    startIntent(SubjectDetailActivity.class,data);
                }
            }
        });
//        ViewGroup parent = (ViewGroup) list.get(position).getParent();
//        parent.removeView(list.get(position));
        container.addView(list.get(position));
        return list.get(position);
    }

    private void startIntent(Class<?> act,String data) {
        Intent intent=new Intent(context,act);
        intent.putExtra("data",data);
        context.startActivity(intent);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(list.get(position));
    }


}
